package pl.piomin.services.gateway;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.PathItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springdoc.core.GroupedOpenApi;
import org.springdoc.core.customizers.OpenApiCustomiser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionLocator;
import org.springframework.context.annotation.Bean;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {

	private static final Logger LOGGER = LoggerFactory.getLogger(GatewayApplication.class);

	public static void main(String[] args) {
		SpringApplication.run(GatewayApplication.class, args);
	}

	@Autowired
	RouteDefinitionLocator locator;

	@Bean
	public List<GroupedOpenApi> apis() {
		List<GroupedOpenApi> groups = new ArrayList<>();
		List<RouteDefinition> definitions = locator.getRouteDefinitions().collectList().block();
		definitions.stream().filter(routeDefinition -> routeDefinition.getId().matches(".*-service")).forEach(routeDefinition -> {
			String name = routeDefinition.getId().replaceAll("-service", "");
			System.out.println("$$$$$$$"+ routeDefinition.getId());
			groups.add(GroupedOpenApi.builder().setGroup(name).pathsToMatch("/" + name + "/**").build());
		});
		System.out.println(groups.size());
		return groups;
	}
/*
	@Bean
	public GroupedOpenApi employeeOpenApi() {
		String paths[] = {"/employee/**"};
		return GroupedOpenApi.builder().setGroup("employee").pathsToMatch(paths)
				.build();
	}
	@Bean
	public GroupedOpenApi departmentOpenApi() {
		String paths[] = {"/department/**"};
		return GroupedOpenApi.builder().setGroup("department").pathsToMatch(paths)
				.build();
	}*/

}
